import 'package:app/api/material-pick/material.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import '../../components/index.dart';
import '../../util/StringUtils.dart';

/// 查询物料清单
class Materials extends StatefulWidget {
  const Materials({super.key,});

  @override
  State<Materials> createState() => _MaterialsState();
}

class _MaterialsState extends State<Materials> {

  String _code = "";

  final FocusNode _searchFocusNode = FocusNode();

  List<dynamic> _list = [];

  @override
  void initState() {
    super.initState();
  }

  void hideKeyBorder() {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus?.unfocus();
    }
  }

  @override
  void dispose() {
    _searchFocusNode.dispose();
    super.dispose();
  }

  /// 搜索
  void search(value){
    MaterialApi.of(context).inventory(value).then((res){
      if(StringUtils.isNotNull(res.data['data'])){
        setState((){
          _list = res.data['data'];
          _code = value;
        });
      }
    });
  }

  ///  站点管理页面
  void goStation(){
    Navigator.pushNamed(context, '/station',arguments:
        {
          'code': _code,
          'stationCode': _list[0]['stationCode'],
          'containerCode': _list[0]['container']
        }).then((_)=> search(_code));
  }

  Widget? getOperation(){
    if(_list.length > 0){
      return SizedBox(
          width: MediaQuery.of(context).size.width,
          child: Padding(
            padding: const EdgeInsets.only(top: 10, left: 20, right: 20),
            child: Button(
              onPressed: StringUtils.isNotNull(_code) ? goStation : null,
              label: '站点更新',
              height: 60,
              fontSize: 18,
            )
          )
      );
    }
    return Container();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBars(
        context: context,
        title: '物料查询',
      ),
      body: GestureDetector(
        onTap: hideKeyBorder,
        child: Column(
          children: [
            BrnSearchText(
              autoFocus: true,
              focusNode: _searchFocusNode,
              hintText: '站点码/托盘码',
              onTextCommit: search
            ),
            Expanded(
              child: ListContainer(
                title: '物料清单',
                length: _list.length,
                children: (index){
                  return [
                    ListTitle(
                      context: context,
                      label: _list[index]['itemName'],
                    ),
                    Horizontal(items: [
                      HorizontalItem(
                        label: '物料编码：',
                        value: _list[index]['itemCode'],
                      ),
                      HorizontalItem(
                        label: '数量：',
                        value: _list[index]['itemQty'],
                        valueBackground: Colors.lightBlueAccent,
                        fontColor: Colors.white,
                      ),
                    ]),
                    Horizontal(items: [
                      HorizontalItem(
                        label: '托  盘  号：',
                        value: _list[index]['container'],
                        valueBackground: Colors.green,
                        fontColor: Colors.white,
                      ),
                    ]),
                  ];
                },
              ),
            ),
            getOperation()!
            // SizedBox(
            //   width: MediaQuery.of(context).size.width,
            //   child: Padding(
            //     padding: const EdgeInsets.only(top: 10, left: 20, right: 20),
            //     child: Button(
            //       onPressed: StringUtils.isNotNull(_code) ? goStation : null,
            //       label: '站点更新',
            //       height: 60,
            //       fontSize: 18,
            //     ),
            //   ),
            // ),
          ],
        )
      ),
    );
  }
}
